﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TCM.DbContextt;
using TCM.Models;
using TCM.Common.Enums;

namespace TCM.Forms
{
    public partial class WorkWithPlans : Form
    {
        private BaseContext db;
        public WorkWithPlans()
        {
            InitializeComponent();

            db = new BaseContext();
            db.Users.Load();
            db.WorkWithTestPlans.Load();
            db.TestPlans.Load();
            RefreshData();
            LoadCombo();
        }

        private void LoadCombo()
        {
            comboBox1.DataSource = Enum.GetValues(typeof(TestPlanStates)).Cast<TestPlanStates>().Select(p => 
                new {Name = Enum.GetName(typeof(TestPlanStates),p), Value = (int)p}).ToList();
            comboBox1.ValueMember = "Value";
            comboBox1.DisplayMember = "Name";
        }


        private void RefreshData()
        {
            dataGridView1.DataSource = db.WorkWithTestPlans.Where(x => x.CurrentUserId == Program.UserId).ToList();
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            var data = db.WorkWithTestPlans.Where(x => x.CurrentUserId == Program.UserId).Where(x => x.TestPlan.Name.Contains(textBox1.Text));
            dataGridView1.DataSource = data.ToList();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1 != null)
            {
                TestPlanStates testPlanStates;
                Enum.TryParse<TestPlanStates>(comboBox1.SelectedValue.ToString(), out testPlanStates);
                var data = db.WorkWithTestPlans.Where(x => x.State == testPlanStates);
                dataGridView1.DataSource = data.ToList();
            }
        }
    }
}
